package cn.metona.mq.core;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * 队列管理器
 * 管理多个队列的创建、销毁、监控等
 */
public class QueueManager {
    private static final Logger logger = Logger.getLogger(QueueManager.class.getName());

    private static final QueueManager INSTANCE = new QueueManager();

    private final ConcurrentHashMap<String, MessageQueue> queues;  // 队列映射
    private final AtomicInteger queueCounter;                      // 队列计数器

    /**
     * 私有构造函数
     */
    private QueueManager() {
        this.queues = new ConcurrentHashMap<>();
        this.queueCounter = new AtomicInteger(0);
    }

    /**
     * 获取单例实例
     */
    public static QueueManager getInstance() {
        return INSTANCE;
    }

    /**
     * 创建队列
     */
    public MessageQueue createQueue(String queueName) {
        return queues.computeIfAbsent(queueName, name -> {
            MessageQueue queue = new MessageQueue(name);
            queueCounter.incrementAndGet();
            logger.info("Queue created: " + name);
            return queue;
        });
    }

    /**
     * 创建队列（可选择是否启用持久化）
     */
    public MessageQueue createQueue(String queueName, boolean enablePersistence) {
        return queues.computeIfAbsent(queueName, name -> {
            MessageQueue queue = new MessageQueue(name, enablePersistence);
            queueCounter.incrementAndGet();
            logger.info("Queue created: " + name + " (persistence: " + enablePersistence + ")");
            return queue;
        });
    }

    /**
     * 获取队列
     */
    public MessageQueue getQueue(String queueName) {
        return queues.get(queueName);
    }

    /**
     * 删除队列
     */
    public boolean deleteQueue(String queueName) {
        MessageQueue queue = queues.remove(queueName);
        if (queue != null) {
            queue.stop();
            queueCounter.decrementAndGet();
            logger.info("Queue deleted: " + queueName);
            return true;
        }
        return false;
    }

    /**
     * 检查队列是否存在
     */
    public boolean existsQueue(String queueName) {
        return queues.containsKey(queueName);
    }

    /**
     * 获取所有队列名称
     */
    public String[] getAllQueueNames() {
        return queues.keySet().toArray(new String[0]);
    }

    /**
     * 获取队列数量
     */
    public int getQueueCount() {
        return queueCounter.get();
    }

    /**
     * 获取所有队列统计信息
     */
    public MessageQueue.QueueStats[] getAllQueueStats() {
        return queues.values().stream()
                .map(MessageQueue::getStats)
                .toArray(MessageQueue.QueueStats[]::new);
    }

    /**
     * 启动所有队列
     */
    public void startAllQueues() {
        queues.values().forEach(MessageQueue::start);
        logger.info("All queues started");
    }

    /**
     * 停止所有队列
     */
    public void shutdown() {
        queues.values().forEach(MessageQueue::stop);
        queues.clear();
        queueCounter.set(0);
        logger.info("All queues shutdown");
    }

    /**
     * 暂停所有队列
     */
    public void pauseAllQueues() {
        queues.values().forEach(MessageQueue::pause);
        logger.info("All queues paused");
    }

    /**
     * 恢复所有队列
     */
    public void resumeAllQueues() {
        queues.values().forEach(MessageQueue::resume);
        logger.info("All queues resumed");
    }
}